﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using Zyg.Utility.Helper;
using Zyg.SSO.Core.Business;

namespace Zyg.SSO.Web.Admin.Role
{
    public partial class AddRolePermission : Zyg.SSO.Web.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!base.Account.HavePermission(Permission.RoleManage))
                {
                    throw new Exception("你无权查看此页面");
                }

                bool IsValid = false;
                Guid roleid = Request.QueryString["RoleID"].ToGuid(out IsValid);

                if (IsValid)
                {
                    RoleIDHF.Value = Request.QueryString["RoleID"].ToString();
                    CheckBox_CheckedBind(new Guid(RoleIDHF.Value));
                    RoleNameLbl.Text = GetRoleName(new Guid(RoleIDHF.Value));
                }
                else
                {
                    throw new Exception("对不起，你所访问的页面不存在");
                }
            }

        }
        protected void CheckBox_CheckedBind(Guid roleid)
        {
            List<Zyg.SSO.Core.Business.Permission> roleper = new List<Zyg.SSO.Core.Business.Permission>();
            roleper = Zyg.SSO.Core.Business.RolePermission.SelectPermissionByRoleID(roleid);

            if (roleper.Contains(Zyg.SSO.Core.Business.Permission.AccountManage))
            {
                UserPermissionCB.Checked = true;
            }
            if (roleper.Contains(Zyg.SSO.Core.Business.Permission.AdminManage))
            {
                AdminPermissionCB.Checked = true;
            }
            if (roleper.Contains(Zyg.SSO.Core.Business.Permission.AppManage))
            {
                AppPermissionCB.Checked = true;
            }
            if (roleper.Contains(Zyg.SSO.Core.Business.Permission.RoleManage))
            {
                RolePermissionCB.Checked = true;
            }
            if (roleper.Contains(Zyg.SSO.Core.Business.Permission.LogManage))
            {
                LogManageCB.Checked = true;
            }
            if (roleper.Contains(Zyg.SSO.Core.Business.Permission.SettingManage))
            {
                SettingManageCB.Checked = true;
            }
        }

        /// <summary>
        /// 获取用户名
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        protected string GetRoleName(Guid roleid)
        {
            Zyg.SSO.Core.Business.Role role = new Zyg.SSO.Core.Business.Role();
            role = Zyg.SSO.Core.Business.Role.Load(roleid);

            return role.Name;
        }

        protected void Save_OnClick(object sender, EventArgs e)
        {
            Zyg.SSO.Core.Business.Role role = Zyg.SSO.Core.Business.Role.Load(new Guid(RoleIDHF.Value));

            if (role == null)
            {
                throw new Exception("当前角色已被删除");
            }

            #region UpdateNewPermission

            List<Zyg.SSO.Core.Business.RolePermission> perList = new List<Zyg.SSO.Core.Business.RolePermission>();

            if (RolePermissionCB.Checked)
            {
                Zyg.SSO.Core.Business.RolePermission per = new Zyg.SSO.Core.Business.RolePermission();
                per.PermissionID = (Guid)Zyg.SSO.Core.Business.Permission.RoleManage;
                per.RoleID = role.Id;
                perList.Add(per);
            }

            if (AdminPermissionCB.Checked)
            {
                Zyg.SSO.Core.Business.RolePermission per = new Zyg.SSO.Core.Business.RolePermission();
                per.PermissionID = (Guid)Zyg.SSO.Core.Business.Permission.AdminManage;
                per.RoleID = role.Id;
                perList.Add(per);
            }

            if (AppPermissionCB.Checked)
            {
                Zyg.SSO.Core.Business.RolePermission per = new Zyg.SSO.Core.Business.RolePermission();
                per.PermissionID = (Guid)Zyg.SSO.Core.Business.Permission.AppManage;
                per.RoleID = role.Id;
                perList.Add(per);
            }

            if (UserPermissionCB.Checked)
            {
                Zyg.SSO.Core.Business.RolePermission per = new Zyg.SSO.Core.Business.RolePermission();
                per.PermissionID = (Guid)Zyg.SSO.Core.Business.Permission.AccountManage;
                per.RoleID = role.Id;
                perList.Add(per);
            }

            if (SettingManageCB.Checked)
            {
                Zyg.SSO.Core.Business.RolePermission per = new Zyg.SSO.Core.Business.RolePermission();
                per.PermissionID = (Guid)Zyg.SSO.Core.Business.Permission.SettingManage;
                per.RoleID = role.Id;
                perList.Add(per);
            }

            if (LogManageCB.Checked)
            {
                Zyg.SSO.Core.Business.RolePermission per = new Zyg.SSO.Core.Business.RolePermission();
                per.PermissionID = (Guid)Zyg.SSO.Core.Business.Permission.LogManage;
                per.RoleID = role.Id;
                perList.Add(per);
            }

            Zyg.SSO.Core.Business.RolePermission.UpdateRolePermission(role, perList);

            WriteBackScript("alert('角色权限修改成功')");

            #endregion
        }

    }
}
